#include <Windows.h>
#include <iostream>

int test1(){

    HANDLE hFile = CreateFile(L"test.txt", GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE) {
        std::cerr << "CreateFile failed. Error: " << GetLastError() << std::endl;
        return 1;
    }

    DWORD bytesWritten;
    char data[] = "Hello, World!";
    WriteFile(hFile, data, sizeof(data), &bytesWritten, NULL);

    if (!FlushFileBuffers(hFile)) {
        std::cerr << "FlushFileBuffers failed. Error: " << GetLastError() << std::endl;
        CloseHandle(hFile);
        return 1;
    }

    CloseHandle(hFile);

    return 0;
}

int test2(){
    HANDLE hMapping = CreateFileMapping(
        INVALID_HANDLE_VALUE,
        NULL,
        PAGE_READWRITE,
        0,
        1024,
        L"SharedMemory");

    if (hMapping == NULL) {
        std::cerr << "CreateFileMapping failed. Error: " << GetLastError() << std::endl;
        return 1;
    }

    LPVOID lpBase = MapViewOfFile(hMapping,
                                  FILE_MAP_WRITE,
                                  0,
                                  0,
                                  1024);

    if (lpBase == NULL) {
        std::cerr << "MapViewOfFile failed. Error: " << GetLastError() << std::endl;
        CloseHandle(hMapping);
        return 1;
    }

    // 在这里可以使用共享内存进行操作

    int *n = (int*)lpBase;
    *n = 100;

    std::cout << *n << std::endl;

    UnmapViewOfFile(lpBase);
    CloseHandle(hMapping);

    return 0;
}

int test3(){


    const char* code = "\x90\x90\x48\xc7\xc0\x01\x00\x00\x00\xcd\x80";  // 简单的示例代码，执行一个系统调用返回 1


        DWORD pid = GetCurrentProcessId();

        std::cout << pid <<std::endl;

        HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);

        if (hProcess == NULL) {
            std::cerr << "Failed to open process. Error: " << GetLastError() << std::endl;
            return 1;
        }

        LPVOID memAddress = VirtualAllocEx(hProcess, NULL, sizeof(code), MEM_COMMIT, PAGE_EXECUTE_READWRITE);

        if (memAddress == NULL) {
            std::cerr << "VirtualAllocEx failed. Error: " << GetLastError() << std::endl;
            CloseHandle(hProcess);
            return 1;
        }

        if (!WriteProcessMemory(hProcess, memAddress, code, sizeof(code), NULL)) {
            std::cerr << "WriteProcessMemory failed. Error: " << GetLastError() << std::endl;
            VirtualFreeEx(hProcess, memAddress, 0, MEM_RELEASE);
            CloseHandle(hProcess);
            return 1;
        }

        HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)memAddress, NULL, 0, NULL);

        if (hThread == NULL) {
            std::cerr << "CreateRemoteThread failed. Error: " << GetLastError() << std::endl;
        } else {
            WaitForSingleObject(hThread, INFINITE);
            CloseHandle(hThread);
        }

        VirtualFreeEx(hProcess, memAddress, 0, MEM_RELEASE);

        CloseHandle(hProcess);

        return 0;

}

int main() {

//    test3();


//    std::cout << printf << std::endl;


    return 0;
}
